package com.mofang.powerdekorhelper.activity.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.model.JVcodeUser;
import com.mofang.powerdekorhelper.persenter.ForgetPasswordPresenter;
import com.mofang.powerdekorhelper.utils.Countdown;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.StringUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.ForgetPasswordView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2016/7/22 0022.
 */


public class ForgetPasswordActivity extends MvpActivity<ForgetPasswordView, ForgetPasswordPresenter> implements ForgetPasswordView {

    @BindView(R.id.forget_password_phone_edit)
    EditText phoneEdit;

    @BindView(R.id.forget_password_vcode_edit)
    EditText vcodeEdit;

    @BindView(R.id.forget_password_get_vcode)
    TextView validateTv;

    String phone;

    Countdown timer;

    @Override
    public ForgetPasswordPresenter initPresenter() {
        return new ForgetPasswordPresenter();
    }

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_forget_pw_layout);
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.forget_password_back_imv, R.id.forget_password_next_btn, R.id.forget_password_get_vcode})
    void OnClick(View v) {
        phone = phoneEdit.getText().toString();
        String vcode = vcodeEdit.getText().toString();
        switch (v.getId()) {
            case R.id.forget_password_get_vcode:
                if (!TextUtils.isEmpty(phone) && phone.length() == 11 && StringUtils.isPhoneNumAvaliable(phone)) {

                    presenter.getIsAccountExists(phone);
                } else {
                    T.showShort(this, "请输入正确的手机号码");
                }
                break;

            case R.id.forget_password_next_btn:

                if (!TextUtils.isEmpty(phone)) {
                    if (!TextUtils.isEmpty(vcode)) {

                        presenter.getForgetPassword(phone, vcode);

                    } else {
                        T.showShort(this, "请输入验证码");
                    }
                } else {
                    T.showShort(this, "请输入手机号");
                }

                break;
            case R.id.forget_password_back_imv:
                finish();
                break;
        }
    }

    @Override
    public void setIsAccountExists(String isAccountExists) {
        if (isAccountExists != null) {

            try {
                JSONObject jsonObject = new JSONObject(isAccountExists);
                String code = jsonObject.getString("code");
                String message = jsonObject.getString("message");
                if (code.equals("0")) {
                    // 账号未被注册
                    T.showShort(this, "抱歉,该帐号未注册");
                } else {
                    // 账号已注册，获取验证码
                    presenter.getMesCode(phone);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setResetPassword(JVcodeUser jVcodeUser) {
        if (jVcodeUser != null) {
            String msg = jVcodeUser.getMessage();
            if (msg.equals("验证成功")) {

                JVcodeUser.User user = jVcodeUser.getResult();
                int id = user.getId();
                SharePerforenceUtils.getInstance(this).setUserID(id);

                startActivity(new Intent(this, ResetPassWordActivity.class).putExtra("Account", phone));

                timer.stop();
            } else {
                T.showShort(this, "验证失败");
            }
        } else {
            T.showShort(this, "验证失败");
        }
    }


    @Override
    public void setMesCode(String msg) {
        if (msg != null & !msg.equals("")) {

            if (msg.contains("成功")) {

                T.showShort(this, "验证码已发送,请稍候");

                timer = new Countdown();
                timer.createTimer(new Countdown.UpDataTime() {
                    @Override
                    public void updataTime(int time) {
                        L.i("SetMesCode", "timer----------------->" + time);
                        validateTv.setText(time + "秒后重发");
                        if (time > 0) {
                            validateTv.setEnabled(false);
                        } else {
                            validateTv.setText("重新发送");
                            validateTv.setEnabled(true);
                        }
                    }
                });

            }
        } else {
            T.showShort(this, "发送失败，请重试");
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toast(CharSequence s) {

    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {

    }

    // 设置获取验证码的背景图片和文字
    private void verifyBtnState(Boolean state, int colorId, String verifyStr) {
        // verifyBtn.setBackgroundResource(drawableId);
        //verifyBtn.setTextColor(FindPassWord.this.getResources().getColor(colorId));
        validateTv.setText(verifyStr);
        validateTv.setEnabled(state);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.stop();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (timer != null) {
            timer.stop();
        }
    }
}
